<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/5/22
 * Time: 11:50
 */

namespace zhtfrontend\modules\cus\components\services;

use common\components\helpers\Fun;
use common\models\datas\CustomerFieldConfig;
use common\models\datas\CustomerFieldConfigRelation;
use yii\base\Component;
use yii\data\Pagination;
use yii\db\Query;


class Cusfc extends Component
{
    public static function GetList($where = [], $page = 1, $pageSize = 12)
    {
        $model = CustomerFieldConfig::find();
        if (!empty($where['code'])) {
            $model->andWhere(['like', 'code', $where['code']]);
        }
        if (!empty($where['m'])) {
            $model->andWhere(['module' => $where['m']]);
        }
        if (!empty($where['status'])) {
            $model->andWhere(['status' => $where['status']]);
        }
        $pages = new Pagination(['totalCount' => $model->count(), 'pageSize' => $pageSize]);
        isset($page) ? $pages->page = $page - 1 : 0;
        $data = $model->offset($pages->offset)->limit($pages->limit)->all();
        $data = Fun::asArray($data, ['admin', 'operate', 'status']);
        return ['data' => $data, 'pages' => $pages];
    }

    public static function doSave($model)
    {
        $site_id = Fun::getSiteId();
        $is_have = CustomerFieldConfig::findOne(['code' => $model->code, 'module' => $model->module]);
        if (!empty($is_have)) {
            $fieldConfigRelation = CustomerFieldConfigRelation::findOne(['customer_field_config_id' => $is_have->id, 'site_id' => $site_id]);
            if (empty($fieldConfigRelation)) {
                Cusfc::addColumns($is_have);
                $fieldConfigRelation = new CustomerFieldConfigRelation();
                $fieldConfigRelation->site_id = $site_id;
                $fieldConfigRelation->customer_field_config_id = $is_have->id;
                $fieldConfigRelation->module = $is_have->module;
                if ($fieldConfigRelation->save(false)) {
                    return ['message' => '添加成功'];
                }
            } else {
                return ['message' => '该分店已经添加该字段'];
            }
        } else {//写成事务处理
            //空，则没有相同code,执行save操作，同时添加customer_field_config_relation表数据
            $model->admin_id = \Yii::$app->user->identity->id;
            if ($model->save(false)) {
                Cusfc::addColumns($model);
                $newFieldConfigRelation = new CustomerFieldConfigRelation();
                $newFieldConfigRelation->customer_field_config_id = $model->id;
                $newFieldConfigRelation->site_id = $site_id;
                $newFieldConfigRelation->module = $model->module;
                $newFieldConfigRelation->save(false);
                return ['message' => '添加成功'];
            }
        }
    }

    //添加配置项同时customer、student表添加字段
    public static function addColumns($model)
    {
        $db = Fun::getSysDb();
        $migration = new Dso();
        $migration->db = $db;
        $table = '{{%' . $model->module . '}}';
        $column = $model->code;
//        $comment = $model->notes . $model->type_value;
        $default_value = $model->default_value;

        //默认值 字段长度
        $migration_str = $model->data_type == 0 ? $migration->string(255) : $migration->smallInteger(6);
        $migration_str = $model->is_required == 1 ? $migration_str->defaultValue($default_value)->notNull() : $migration_str->defaultValue($default_value);
        $migration_str = $migration_str->comment('');

        $migration->addColumn($table, $column, $migration_str);

    }

    public static function GetDataList($module = 'customer')
    {
        $data = (new Query())->select('a.*,b.status as status')
            ->from('{{%customer_field_config}} a')
            ->leftJoin('{{%customer_field_config_relation}} b', "b.customer_field_config_id = a.id")
            ->where(['b.site_id'=>Fun::getSiteId(),'b.status'=>1,'a.module'=>$module])
            ->all();
        return $data;
    }



}